<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="formName">
        <el-input
            v-model="queryParams.formName"
            placeholder="请输入流程标题"
            clearable
            style="width: 300px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="待办人" prop="createBy">
        <el-input
            v-model="queryParams.createBy"
            placeholder="请输入待办人"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="haveDownList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
          width="300"
          label="流程标题"
          align="left"
          prop="formName"
          :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleProcessPreview(scope.row)">
            {{scope.row.formName}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column width="200" show-overflow-tooltip label="任务名称" align="left" prop="name"/>
      <el-table-column width="120" show-overflow-tooltip label="待办人" align="left" prop="assigneeName">
        <template #default="scope">
          <el-tag v-if="scope.row.currentAssignees&& scope.row.currentAssignees.length>0" v-for="item in scope.row.currentAssignees">{{item.name || item.code}}</el-tag>
          <span v-else> - </span>
        </template>
      </el-table-column>
      <el-table-column width="120" show-overflow-tooltip label="停留时间" align="left" prop="stayHour"/>
      <el-table-column width="100" show-overflow-tooltip label="状态" align="left" prop="processStatusName"/>
      <el-table-column width="200" show-overflow-tooltip label="任务ID" align="center" prop="taskId"/>
      <el-table-column width="150" show-overflow-tooltip label="业务单号" align="center" prop="businessKey"/>
      <el-table-column width="200" show-overflow-tooltip label="流程实例ID" align="center" prop="processInstanceId"/>
      <el-table-column width="200" show-overflow-tooltip label="任务创建时间" align="center" prop="createTime"/>
      <el-table-column width="200" show-overflow-tooltip label="流程发起时间" align="center" prop="processStartTime"/>

      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              link
              type="primary"
              icon="Picture"
              @click="handleBpmnPreview(scope.row)"
              v-hasPermi="['system:notice:edit']"
          >流程图
          </el-button>
          <el-button
              link
              type="primary"
              icon="ChatLineSquare"
              @click="handleApproveHistoryPreview(scope.row)"
              v-hasPermi="['system:notice:remove']"
          >审批记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 预览流程对话框 -->
    <el-dialog width="1000px"
               :title="bpmnViewerTitle"
               v-model="bpmnViewerVisible"
               append-to-body
               class="bpmn-viewer-body">
      <div style="height: 500px;">
        <BpmnViewer :proc-inst-id="previewProcInstId" ref="bpmnViewerRef" />
      </div>
    </el-dialog>

    <!-- 预览流审批记录对话框 -->
    <el-dialog
        :title="approveHistoryTitle"
        v-model="approveHistoryVisible"
        width="1000px"
        :footer="null"
        append-to-body>
      <div>
        <ApproveHistory :proc-inst-id="previewProcInstId" />
      </div>
    </el-dialog>

    <!-- 审批/查看流程对话框 -->
    <el-dialog class="process-dialog-wrapper" :title="processPreviewTitle" v-model="processPreviewVisible" fullscreen append-to-body>
      <div style="position: relative; height: 100%;">
        <process-wrapper
            :model-key="modelKey"
            :proc-inst-id="procInstId"
            :task-id="taskId"
            view-type="read"
            ref="processWrapperRef"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="HaveDown">
import {queryApplyedTask} from "@/api/flowable/bpmn/modelInfo";
import BpmnViewer from "@/views/flowable/components/BpmnViewer.vue";
import ApproveHistory from "@/views/flowable/components/ApproveHistory.vue";
import ProcessWrapper from "@/views/process/components/ProcessWrapper.vue";
const { proxy } = getCurrentInstance();

const approveHistoryTitle = ref('');
const approveHistoryVisible = ref(false);
const previewProcInstId = ref('');
const bpmnViewerTitle = ref('');
const bpmnViewerVisible = ref(false);

const processPreviewTitle = ref('');
const processPreviewVisible = ref(false);
const modelKey = ref('');
const procInstId = ref('');
const taskId = ref('');

// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 表格数据
const haveDownList = ref([]);
// 弹出层标题
const title = ref("");

// 是否显示弹出层
const open = ref(false);
// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  formName: ''
});
const getList = () => {
  loading.value = true;
  queryApplyedTask(unref(queryParams)).then(response => {
    haveDownList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

function handleProcessPreview(row){
  processPreviewTitle.value = `审批预览 - ${row.formName}`
  processPreviewVisible.value = true;
  procInstId.value = row.processInstanceId;
  // taskId.value = row.taskId;
  modelKey.value = row.processDefinitionKey;
}

function handleBpmnPreview (row){
  bpmnViewerTitle.value = "流程预览-" + row.formName;
  bpmnViewerVisible.value = true;
  previewProcInstId.value = row.processInstanceId;
}

function handleApproveHistoryPreview (row) {
  approveHistoryVisible.value = true;
  previewProcInstId.value = row.processInstanceId;
  approveHistoryTitle.value = `审批记录 - ${row.formName}`;
}
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
}

getList();


</script>

<style lang="scss">
.process-dialog-wrapper {
  .el-dialog__body {
    position: relative;
    padding: 0 20px;
    height: calc(100vh - 80px); /* 100vh代表全屏，124px是el-dialog头部以及footer的高度，需要根据实际情况调整 */
    overflow-y: auto; /* 添加滚动条 */
  }
}

</style>
